package com.platform.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.base.R;
import com.platform.base.mvc.BaseController;
import com.platform.dto.UserRole;
import com.platform.exception.BizException;
import com.platform.logger.annotation.ApiLog;
import com.platform.service.IUserRole;
import com.platform.utils.StringUtils;

import cn.hutool.core.map.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 
 * <p> Title:Yhjs Mapper</p>
 * <p> Description:  用户角色表</p>
 * <p> Copyright: Copyright (c) 2019 </p>
 * <p> Company: </p>
 *
 * @author wys
 * @version 1.0
 */
@Controller
@RequestMapping("/userRole") 
@Api(tags="OMS-用户/角色关联模块")
public class UserRoleController extends BaseController{
	
	@Autowired
    private IUserRole services;

    /**
     *  
     * selectAll(加载数据)
     * 
     * @param request
     * @param response
     * @param session
     * @return Object 可以是 JSON 或者 XML
     * @exception 
     * @version  1.0.0
     */
    @RequestMapping(value="data",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="获取所有数据")
    public Object selectAll(HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        Map<String,Object> map = MapUtil.newHashMap();
        List<UserRole> servicesList = services.selectAll(map);
        return R.success(servicesList);
    }
    
    /**
     *  
     * delete(删除数据)
     * 
     * @param request
     * @param response
     * @param session
     * @return Object 可以是 JSON 或者 XML
     * @exception 
     * @version  1.0.0
     */
    @ApiLog(value="删除角色信息",desc="删除系统角色信息")
    @RequestMapping(value="delete",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="删除数据")
    public Object delete(String ids,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
    	boolean isSuccess = false;
    	String[] idArray = ids.split(",");
    	for(int i=0;i<idArray.length;i++) {
    		UserRole dto = services.get(idArray[i]);
    		isSuccess = services.delete(dto);
    	}
    	if(isSuccess){
    		return R.success();
    	}else {
    		return R.fail();
    	}
    	
    }
    
    /**
     *  
     * insert(新增数据)
     * 
     * @param request
     * @param response
     * @param session
     * @return Object 可以是 JSON 或者 XML
     * @exception 
     * @version  1.0.0
     */
    @ApiLog(value="新增系统角色",desc="系统用户/角色数据保存")
    @RequestMapping(value="save",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="新增数据")
    public Object insert(@Valid UserRole dto,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        dto.setCreater(getName());
        dto.setCreateDate(new Date(System.currentTimeMillis()));
        if(services.insert(dto)) {
        	return R.success();
    	}else {
    		return R.fail();
    	}
    }
    
    @ApiLog(value="绑定用户与角色关系",desc="系统用户/角色关系绑定")
    @RequestMapping(value="userRoleInsert",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="用户、角色关系绑定")
    public Object userRoleInsert(String usercode,String rolecode,HttpServletRequest request,HttpServletResponse response,HttpSession session){
    	try{
    		List<String> roleCodeList=StringUtils.StringToList(usercode, ",");
    		List<UserRole> userRoleList= new ArrayList<UserRole>();
    		for(String str:roleCodeList){
    			UserRole u=new UserRole();
    			u.setUsercode(Long.valueOf(Long.valueOf(str)));
    			u.setRolecode(Long.valueOf(rolecode));
    			u.setCreateDate(new Date(System.currentTimeMillis()));
    			u.setCreater(getName());
    			userRoleList.add(u);
    			//去除系统重已存在重复的数据
    			Map<String,Object> map = MapUtil.newHashMap();
    	    	map.put("usercode", str);
    	    	map.put("rolecode", rolecode);
    	    	services.deleteUserRoleByUserCodeAndRoleCode(map);
    		}
		    services.insertRoleUser(userRoleList);
		    return R.success();
    	}catch(NumberFormatException e){
    		return R.fail();
    	}catch (BizException e) {
    		return R.fail();
        }
    }
    
    @ApiLog(value="查询已用户拥有的角色信息",desc="查询已用户拥有的角色信息")
    @RequestMapping(value="selectUserByRoleCode",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="查询指定角色下拥有的用户数据")
    public Object selectUserByRoleCode(Long roleCode,HttpServletRequest request,HttpServletResponse response,HttpSession session){
    	return R.success(services.selectUserByRoleCode(roleCode));
    }
    
    @ApiLog(value="查询没有被分配的用户信息",desc="查询没有被分配的用户信息")
    @RequestMapping(value="selectUserNotHavaRoleByRoleCode",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="根据角色、部门编号查询未分配到角色的用户列表")
    public Object selectUserNotHavaRoleByRoleCode(Long rolecode,Long deptCode){
    	Map<String,Object> map = MapUtil.newHashMap();
    	map.put("rolecode", rolecode);
    	map.put("deptCode", deptCode);
    	return R.success(services.selectUserNotHavaRoleByRoleCode(map));
    }
    @RequestMapping(value="deleteUserRoleByUserCodeAndRoleCode",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="删除指定角色下的指定用户信息")
    public Object deleteUserRoleByUserCodeAndRoleCode(String usercode,String rolecode){
    	Map<String,Object> map = MapUtil.newHashMap();
    	map.put("usercode", usercode);
    	map.put("rolecode", rolecode);
    	if(services.deleteUserRoleByUserCodeAndRoleCode(map)>0) {
    		return R.success();
    	}else {
    		return R.fail();
    	}
    }
    /**
     *  
     * selectAll(修改数据)
     * 
     * @param request
     * @param response
     * @param session
     * @return Object 可以是 JSON 或者 XML
     * @exception 
     * @version  1.0.0
     */
    @ApiLog(value="修改角色信息",desc="系统用户/角色数据修改")
    @RequestMapping(value="edit",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="修改数据")
    public Object update(@Valid UserRole dto, HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
    	dto.setLastModifyDate(new Date(System.currentTimeMillis()));
		dto.setLastModifier(getName());
        if(services.update(dto)) {
        	return R.success();
        }else {
        	return R.fail();
        }
    }
    
    
    /**
     *  
     * selectAll(获取单条数据)
     * 
     * @param request
     * @param response
     * @param session
     * @return Object 可以是 JSON 或者 XML
     * @exception 
     * @version  1.0.0
     */
    @RequestMapping(value="find",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="获取单条数据")
    public Object get(String code, HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        return services.get(code);
    }
}
